<?php
class AccountAction extends CommonAction {
	public function index(){
		$this->salelog();
	}
	//销售日志，统计
	public function salelog(){
		$ordersql = M('Order') -> where(array('site_id' => $this->siteid, 'status' => 2)) -> Field('id') -> buildSql();
        $pagesize = 10;
        if ($this->_get('pagesize')) {
            $pagesize = $this->_get('pagesize');
        }
        $listmap = 's.site_id = '.$this->siteid.' and s.order_id in('.$ordersql.')';//站点、商品属于该站点
        if ($this->_get('stime') || $this->_get('etime')) {
            $stime = strtotime($this->_get('stime'));
            $etime = strtotime($this->_get('etime')) + 24 * 3600;
            if ($this->_get('stime')) {
                $listmap .= " and s.create_time >= '" . $stime . "'";
            }
            if ($this->_get('etime')) {
                $listmap .= " and s.create_time <= '" . $etime . "'";
            }
            if ($this->_get('etime') && $this->_get('stime')) {
                $listmap .= " and s.create_time <= '" . $etime . "'";
                $listmap .= " and s.create_time >= '" . $stime . "'";
            }
        }
        if (!session(C('ADMIN_AUTH_KEY')) && $this->is_suppierids > 0) {
            $listmap .= ' and s.supplier_id in(' . $this->suppierids . ')';
            $this->suppier = M('Supplier')->where('site_id=' . $this->siteid . ' and id in(' . $this->suppierids . ')')->select();
        } else {
            $this->suppier = M('Supplier')->where('site_id=' . $this->siteid)->select();
        }
        if (isset($_GET['supplier_id']) && intval($_GET['supplier_id']) > 0) {
            $listmap .= " and s.supplier_id=" . intval($_GET['supplier_id']);
            $this->assign('supplier_id', intval($_GET['supplier_id']));
        }
        if ($this->_get('keywords')) {
            $listmap .= " and s.title like '%" . $this->_get('keywords') . "%'";
        }
        $orderDB = D('Sale');
        $count = $orderDB->alias('s')->join('left join __ORDER__ AS o on o.id = s.order_id')->where($listmap)->count();
        //实例化分页类
        import('ORG.Util.Page');
        $p = new Page($count, $pagesize);
        $list = $orderDB->alias('s')->join('left join __ORDER__ AS o on o.id = s.order_id')->join('left join think_supplier AS r on r.id = s.supplier_id')->where($listmap)->order('order_id DESC')->group("order_id")->limit($p->firstRow . ',
' . $p->listRows) -> Field('s.*,o.ordernumber,r.susername,s.total AS price') -> select();
		// $memberdb = M('Member');//获取头像，昵称
        //->join('left join think_member_account_log AS m on m.order_id = s.order_id') 
        foreach ($list as $key => $value) {
            //$list[$key]['affmoney']=M('member_account_log')->where('order_id = '.$value['order_id'])->getField('changenum');
            $list[$key]['affmoney']=M('member_account_log')->where('order_id = '.$value['order_id']." and product_id != 0 and attr = '' ")->order('order_id')->sum('changenum');
            //$affmoney+=$list[$key]['affmoney'];//分成的钱的总和
            $list[$key]['rest_money']=sprintf("%.2f",$value['price']-$list[$key]['affmoney']);
        }     
		$pages = $p->show();
		$this->assign('list', $list);
        $this->assign('pages', $pages);
		$this->display();
	}

     //金额总计
    public function totalamount(){
        $id = $this->_get('id');//获取选中的所有id
        $order_id = explode('_',$id);//拆分id
        array_pop($order_id); //去除最后一个空id

        $price='';
        $newDB=M('sale');
        // dump($order_id);
        foreach ($order_id as $key => $value) {
            $listmap.=' s.id = '.$value.' or ';
        }
        $listmap=substr($listmap,0,strlen($listmap)-4);
        $listmap="s.site_id = " . $this->siteid .' and '.$listmap;
        // dump($listmap);
        $mission=$newDB->alias('s')->join('left join __ORDER__ AS o on o.id = s.order_id')->join('left join think_supplier AS r on r.id = s.supplier_id')->where($listmap)->order('order_id DESC')-> Field('s.*,o.ordernumber,r.susername,s.price AS price') -> select();
 
        foreach ($mission as $k => $v) {
            //$mission[$k]['affmoney']=M('member_account_log')->where('order_id = '.$v['order_id'])->getField('changenum');
            $mission[$k]['affmoney']=M('member_account_log')->where('order_id = '.$v['order_id']." and product_id != 0 and attr = '' ")->order('order_id')->sum('changenum');
            // $affmoney+=$mission[$k]['affmoney'];//分成的钱的总和
            $mission[$k]['rest_money']=sprintf("%.2f",$v['price']-$mission[$k]['affmoney']);
            $price+=$mission[$k]['rest_money'];
        }    
        
        $this->assign('list', $mission);
        $this->assign('price',sprintf("%.2f", $price));
        $this->display();
    }
}